//
//  XJSleepingSet.m
//  ttpod
//
//  Created by dllo on 16/3/22.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "XJSleepingSet.h"

@interface XJSleepingSet () <UITableViewDelegate,UITableViewDataSource,UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *arrForLabel;

@property (nonatomic, copy) NSString *countDown; //倒计时时间

@property (nonatomic, strong) UILabel *headerOfLabel;

@property (nonatomic, strong) UIPickerView *pickView;

@end

@implementation XJSleepingSet

{
    
    NSInteger timeMinute;
    
    NSInteger timeSec;
    

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.arrForLabel = @[@"关闭",@"10分钟",@"20分钟",@"30分钟",@"60分钟",@"90分钟",@"自定义"];
    
   
   /**只创建一次 label, 保证进入此页面是 self.header是同一个. */
    static UILabel *label = nil;
    
    if (label == nil) {
        
        label = [[UILabel alloc] init];
        
        label.text = @"计时完毕后,将暂停播放";
        
        self.headerOfLabel = label;
        
    }else {
        
        
        self.headerOfLabel = label;
    }
    
    
    self.navigationItem.title  = @"睡眠设置";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
   
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool1"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    return self.arrForLabel.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];

    cell.textLabel.text = self.arrForLabel[indexPath.row];
    
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
 
    
    self.headerOfLabel.frame = self.view.bounds;
    
    self.headerOfLabel.backgroundColor = [UIColor grayColor];
    
    
    
    self.headerOfLabel.textAlignment = NSTextAlignmentCenter;
    
    return self.headerOfLabel;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
  ;
  
    
  static  NSTimer *timer = nil;
    
    if (indexPath.row != 0 && indexPath.row != 6) {
        
        NSString *str = [[self.arrForLabel objectAtIndex:indexPath.row]substringToIndex:2];
        
        timeMinute = [str integerValue];
        timeSec = 60;
        
        
        if (timer == nil) {
            
            timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerWillWorking:) userInfo:nil repeats:YES];
            
            [timer fire];
        }else {
            
            [timer invalidate];
            
            timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerWillWorking:) userInfo:nil repeats:YES];
            
            [timer fire];
            
        }
    
        
    }
    
    if (indexPath.row == 6) {
        
        
        
        UIView *backView = [[UIView alloc] initWithFrame:self.view.bounds];
        
        /**添加选择器头部视图. */
        UIView *pickHeader = [[UIView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height/2 - 40, self.view.frame.size.width, 40)];
        
        pickHeader.backgroundColor =[UIColor grayColor];
        
        pickHeader.alpha = 1;
        
        
        [self.view addSubview:backView];
        
        [self.view addSubview:pickHeader];
        
        backView.backgroundColor =[UIColor blackColor];
        
        backView.alpha = 0.4;
        
        self.pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2)];
        
        
        
        self.pickView.backgroundColor = [UIColor whiteColor];
        
        [self.view addSubview:self.pickView];
        
        
        UIButton *buttomOfCancal = [UIButton buttonWithType:UIButtonTypeSystem];
        
        [buttomOfCancal setTitle:@"取消" forState: UIControlStateNormal];
        buttomOfCancal.frame = CGRectMake(0,0,40, 40);
        
        UIButton *buttonOfTure = [UIButton buttonWithType:UIButtonTypeSystem];
        
        
        [buttomOfCancal addTarget:self action:@selector(touchOfCancalAction:)     forControlEvents:UIControlEventTouchUpInside]; // 添加点击事件
        
        [buttonOfTure addTarget:self action:@selector(touchOfTureAction:) forControlEvents:UIControlEventTouchUpInside]; //添加点击事件
        
        [buttonOfTure setTitle:@"确定" forState: UIControlStateNormal];
        
        buttonOfTure.frame = CGRectMake(self.view.frame.size.width - 40,0,40, 40);
        
        [pickHeader addSubview:buttomOfCancal];
        [pickHeader addSubview:buttonOfTure];
        
# pragma mark -- 选择器代理方法
        
       self.pickView.delegate = self;
        
        self.pickView.dataSource = self;
        
        
    }
    
    if (indexPath.row == 0) {
        
        [timer invalidate];
        self.headerOfLabel.text = @"计时完毕后,将暂停播放";
        
    }
    
    
    
}


- (void)touchOfCancalAction:(UIButton *)button {
    
    NSLog(@"cancal");
    
    /*移除所有视图. */
    
    [self.pickView removeFromSuperview];
    
    NSArray *arrView = [self.view subviews];
    
    for (UIView *view in arrView) {
        
        if (![view isKindOfClass:[UITableView class]]) {
            
            [view removeFromSuperview];
            
        }
        
    }
    
    
}


- (void)touchOfTureAction:(UIButton *)button {
    
    
    NSLog(@"ture");
    
}


- (void)timerWillWorking:(NSTimer *)timer {
    
    if (timeSec == 0) {
        
        timeSec = 60;
        
        timeMinute --;
        
    }
    
    if (timeMinute == 0 && timeSec == 0) {
        
        [timer invalidate];
        
       
        
        self.headerOfLabel.text = @"计时结束后,将暂停播放歌曲";
        
    }else {
        
      
        
        timeSec --;
        
    
        self.headerOfLabel.text = [NSString stringWithFormat:@"%ld分%ld秒后结束播放",timeMinute-1,timeSec];
        
        
        
    
        
    }
    
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    
    if (component == 0) {
        
        
        return 24;
    }else {
        
        
        return 60;
    }
    
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    
    
    NSString *str= [NSString stringWithFormat:@"%ld",row];
    
    
    return str;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    
    return 2;
}

- (void)viewWillAppear:(BOOL)animated {
    
  
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
